const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: "",
    timeLimit: "", //限时团
    showcountDown: "00 : 00 : 00",//限时团倒计时
    loadingStatus: false,
    not: false,
    hotProduct: { //热销产品
      loadingStatus: false,
      page: 1,
      list: ""
    },
    recommendProduct: { //店长推荐
      loadingStatus: false,
      page: 1,
      list: ""
    },
    nowProduct: { //最新上线
      loadingStatus: false,
      page: 1,
      list: ""
    },
    shopping: { //商城列表
      page: 1,
      list: "",
      loadingStatus: false
    },
    show: ''
  },
  // 横向列表加载更多
  rowSrcroll(e) {
    let that = this;
    let page = 1;
    let listType = e.target.dataset.type;
    if ((listType == 3 && that.data.hotProduct.loadingStatus) || (listType == 2 && that.data.recommendProduct.loadingStatus) || (listType == 4 && that.data.nowProduct.loadingStatus)) {
      return;
    }
    if (listType == 3) {
      page = that.data.hotProduct.page
      that.setData({
        'hotProduct.loadingStatus': true
      })
    } else if (listType == 2) {
      page = that.data.recommendProduct.page
      that.setData({
        'recommendProduct.loadingStatus': true
      })
    } else if (listType == 4) {
      page = that.data.nowProduct.page
      that.setData({
        'nowProduct.loadingStatus': true
      })
    }
    page = ++page;
    wx.request({
      url: app.buildUrl('/index/active-list'),
      header: app.buildHeader(),
      data: { type: listType, page: page },
      success(res) {
        if (res.data.code == 200) {
          let list = res.data.data;
          if (listType == 2) {
            if (list.length != 0) {
              that.setData({
                'recommendProduct.loadingStatus': false,
                'recommendProduct.page': page,
                'recommendProduct.list': that.data.recommendProduct.list.concat(list)
              })
            } else {
              that.setData({
                'recommendProduct.loadingStatus': true
              })
            }
          } else if (listType == 3) {
            if (list.length != 0) {
              that.setData({
                'hotProduct.loadingStatus': false,
                'hotProduct.page': page,
                'hotProduct.list': that.data.hotProduct.list.concat(list)
              })
            } else {
              that.setData({
                'hotProduct.loadingStatus': true
              })
            }
          } else if (listType == 4) {
            if (list.length != 0) {
              that.setData({
                'nowProduct.loadingStatus': false,
                'nowProduct.page': page,
                'nowProduct.list': that.data.nowProduct.list.concat(list)
              })
            } else {
              that.setData({
                'nowProduct.loadingStatus': true
              })
            }
          }
        } else {
          page = --page;
          if (listType == 2) {
            that.setData({
              'recommendProduct.loadingStatus': false,
              'recommendProduct.page': page
            })
          } else if (listType == 3) {
            that.setData({
              'hotProduct.loadingStatus': false,
              'hotProduct.page': page
            })
          } else if (listType == 4) {
            that.setData({
              'nowProduct.loadingStatus': false,
              'nowProduct.page': page
            })
          }
        }
      }
    })
  },
  //商城加载更多
  load() {
    let that = this;
    if (that.data.shopping.loadingStatus) { return; }
    if (that.data.not) { return; }
    that.setData({
      'shopping.loadingStatus': true
    })
    let page = that.data.shopping.page;
    page = ++page;
    wx.showLoading({
      title: '正在加载更多',
    })
    wx.request({
      url: app.buildUrl('/index/active-list'),
      header: app.buildHeader(),
      data: { type: 0, page: page },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              not: true
            })
          } else {
            that.setData({
              'shopping.list': that.data.shopping.list.concat(res.data.data),
              'shopping.page': page
            })
          }
        } else {
          app.showToast(res.data.msg)
        }
        that.setData({
          'shopping.loadingStatus': false
        })
      }
    })
  },
  //授权
  onGotUserInfo(e) {
    let that = this;
    // console.log(e.detail)
    if (e.detail.userInfo) {
      that.setData({
        show: false,
      });
      wx.setStorageSync('status', 200);
      wx.request({
        url: app.buildUrl('/user/save-user-info'),
        header: app.buildHeader(),
        method: 'POST',
        data: {
          url: e.detail.userInfo.avatarUrl,
          nickname: e.detail.userInfo.nickName
        },
        success: res => {
          // wx.redirectTo({
          //   url: '../index/index'
          // })
          if (res.data.code == 200) {
            that.loadUrl();
          } else {
            app.showToast('!200')
          }
        }
      })
    }
  },
  //加载数据
  loadUrl() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    //获取banner
    wx.request({
      url: app.buildUrl('/index/banner-list'),
      header: app.buildHeader(),
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            banner: res.data.data
          })
        } else {
          app.showToast(res.data.msg)
        }
        wx.hideLoading();
      }
    })
    
    //获取首页产品列表
    let typeArr = [0, 1, 2, 3, 4]; //0 商城, 1 限时团, 2 店长推荐, 3 热门活动, 4 新品上线
    typeArr.forEach(i => {
      wx.request({
        url: app.buildUrl('/index/active-list'),
        header: app.buildHeader(),
        data: { type: i },
        success(res) {
          if (res.data.code == 200) {
            if (i == 2) {
              that.setData({
                'recommendProduct.list': res.data.data
              })
            } else if (i == 3) {
              that.setData({
                'hotProduct.list': res.data.data
              })
            } else if (i == 1) {
              that.setData({
                timeLimit: res.data.data
              })
              if (res.data.data) {
                if (that.data.timeLimit.length > 0) {
                  let timeLimit = that.data.timeLimit.length > 3 ? that.data.timeLimit.slice(0, 3) : that.data.timeLimit;
                  for (let i = 0; i < timeLimit.length; i++) {
                    // 调用倒计时
                    app.countDown({
                      date: timeLimit[i].end_time * 1000,
                      fun: (options) => {
                        timeLimit[i].showcountDown = options.d >= 1 ? `${options.d}天  ${options.h} : ${options.m} : ${options.s}` : `${options.h} : ${options.m} : ${options.s}`;
                        that.setData({
                          timeLimit: timeLimit
                        })
                      },
                      endFun: () => {
                        timeLimit[i].showcountDown = "活动已结束";
                        that.setData({
                          timeLimit: timeLimit
                        })
                      }
                    })
                  }
                }
              }
            } else if (i == 4) {
              that.setData({
                'nowProduct.list': res.data.data
              })
            } else if (i == 0) {
              that.setData({
                'shopping.list': res.data.data
              })
            }
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    app.getUserInfoFirst().then(function (res) {
      if (res.status == 200) {
        if (wx.getStorageSync("status") != 200) {
          that.setData({ show: true })
        } else if (wx.getStorageSync("status") == 200) {
          that.setData({ show: false })
          that.loadUrl();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    this.load()
  }
})